//
//  REHelpViewController.m
//  录音
//
//  Created by ywy on 2016/12/23.
//  Copyright © 2016年 Wely. All rights reserved.
//

#import "REHelpViewController.h"
#import "REHelpFirstTableViewCell.h"
#import "REHelpTwoTableViewCell.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "RECheckLogin.h"
#import "REquestion.h"
#import "REProblemDetailsViewController.h"
#import "RETabBarViewController.h"


@interface REHelpViewController ()<UITableViewDelegate,UITableViewDataSource,helpAndfeedbackDelegate>
    
@property(nonatomic,strong) NSMutableArray * dataArr;



@property(nonatomic,copy) NSString * textViewStr;

@property(nonatomic,copy) NSString * phoneStr;


@end

@implementation REHelpViewController

-(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
    RETabBarViewController * tabBar = (RETabBarViewController *)self.tabBarController;
    //自定义隐藏Tabbar
    [tabBar HideTabarView:YES];
    
    UIImageView * firstImageView  =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"帮助与反馈"]];
    firstImageView.frame = CGRectMake(0, 0, 37, 17);
    self.navigationItem.titleView=firstImageView;
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(backPop)];
    [UIApplication sharedApplication].statusBarHidden = YES;
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = true;
    self.navigationItem.leftBarButtonItem = rightItem;
}
-(void)backPop
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    [[NSNotificationCenter defaultCenter]  addObserver:self
//                                              selector:@selector(keyShow) name:UIKeyboardDidShowNotification object:nil];
//    [[NSNotificationCenter defaultCenter]  addObserver:self
//                                              selector:@selector(keyHide) name:UIKeyboardDidShowNotification object:nil];
    
    
    self.dataArr = [NSMutableArray new];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.helpTableView.delegate =self;
    self.helpTableView.dataSource =self;
    self.helpTableView.separatorStyle = 0;
    [self.helpTableView registerNib:[UINib nibWithNibName:@"REHelpFirstTableViewCell" bundle:nil] forCellReuseIdentifier:@"REHelpFirstTableViewCell"];
    [self.helpTableView registerNib:[UINib nibWithNibName:@"REHelpTwoTableViewCell" bundle:nil] forCellReuseIdentifier:@"REHelpTwoTableViewCell"];
    UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, REScreenW, 100)];
    UIButton * footButton = [UIButton  buttonWithType:UIButtonTypeSystem];
    footButton.frame = CGRectMake(REScreenW/2-71, 5, 142, 47);
    [footButton setBackgroundImage:[UIImage imageNamed:@"submit"] forState:UIControlStateNormal];
    [footButton addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:footButton];
    self.helpTableView.tableFooterView = footView;
    [self requestUrl];
    
    // Do any additional setup after loading the view from its nib.
}
-(void)btnClick
{
    __weak typeof(self)  weakSelf = self;
    if ([RECheckLogin isValidateMobile:self.phoneStr] == NO && self.phoneStr.length != 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码"];
        return;
    }
    NSMutableDictionary * dict = [NSMutableDictionary new];
    dict[@"cmd"] = @"upComplain";
    dict[@"uid"] = [UserDefaults objectForKey:@"userid"];
    dict[@"ComplainContent"]  = self.textViewStr;
    if (self.phoneStr.length != 0 || !self.phoneStr) {
         dict[@"tellphone"] = self.phoneStr;
    }
    [SVProgressHUD showWithStatus:@"问题 提交中...."];
    [RECheckLogin POST:REBaseUrl parameters:dict progress:^(CGFloat proser) {
    } success:^(id responseObject) {
        
        
        if ([responseObject[@"result"] isEqualToString:@"0"]) {
            [SVProgressHUD showSuccessWithStatus:responseObject[@"resultNote"]];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else
        {
            [SVProgressHUD showErrorWithStatus:responseObject[@"resultNote"]];
        }
            [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"查看网络！！！！"];
    }];
}
-(void)requestUrl
{
    __weak typeof(self) weakSelf = self;
    NSMutableDictionary * dict = [NSMutableDictionary new];
    dict[@"cmd"] = @"getQuestionList";
    [SVProgressHUD showWithStatus:@"正在加载中......"];
    [RECheckLogin POST:REBaseUrl parameters:dict progress:^(CGFloat proseer) {
        
    } success:^(id responseObject) {
        
        
        if ([responseObject[@"result"] isEqualToString:@"0"]) {
            
            
            for (NSDictionary * dict in responseObject[@"questionList"]) {
                
                
                REquestion * model = [[REquestion alloc] init];
                model.questionId = dict[@"questionId"];
                model.questionName = dict[@"questionName"];
                [weakSelf.dataArr addObject:model];
                
            }
            
            [SVProgressHUD dismiss];
            [weakSelf.helpTableView reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:responseObject[@"resultNote"]];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"查看网络!!!"];
    }];
}
    
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 2;
    }
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.row !=0) {
            REHelpTwoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"REHelpTwoTableViewCell"];
            cell.selectionStyle = 0;
            cell.delegate =self;
            return cell;
        }else{
            REHelpFirstTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"REHelpFirstTableViewCell"];
            if (self.dataArr.count) {
                      [cell showdata:self.dataArr];
            }
            cell.selectionStyle = 0;
            cell.problemsClick = ^(NSInteger number,REquestion*model)
            {
                REProblemDetailsViewController * problemVC = [[REProblemDetailsViewController alloc] init];
                problemVC.Model = model;
                [self.navigationController pushViewController:problemVC animated:YES];
            };
            return cell;

        }
        return nil;
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.row == 0) {
             return 300;
        }else
        {
            return 210;
        }
}

-(void)phoneStr:(NSString *)phoneStr
{
    self.phoneStr = phoneStr;
    
}
-(void)questStr:(NSString *)questStr
{
    self.textViewStr = questStr;
}
//
//-(void)keyShow
//{
////       self. centerPoint = self.helpTableView.center;
////        self.helpTableView.center = CGPointMake(REScreenW/2, 329);
////    self
////    self.helpTableView.frame = CGRectMake(0, -340, REScreenW, REScreenH);
//////    self.helpTableView.scrollIndicatorInsets =  UIEdgeInsetsMake(-320, 0, -320, 0);
//    
//}
//
//-(void)keyHide
//{
//     self.helpTableView.center = self.view.center;
//}

    

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
